﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using JohnnySins.DAL.Repositories;

namespace JohnnySins.Code
{
    public class Avc
    {
        public static void AvcSettings(Page page, string userName)
        {
            int randomVar = -1;
            if (!string.IsNullOrEmpty(page.Request.QueryString["randomVar"]))
            {
                if (!int.TryParse(page.Request.QueryString["randomVar"], out randomVar))
                    randomVar = -1;
            }

            using (var repo = RepositoryFactory.ResolveRepository<ParameterSiteRepository>())
            {
                //################ AVConference 1.2 Configuration file ####################
                //######################## MANDATORY FIELDS #########################
                //connectionstring:String
                //description: the rtmp connection string to the avconference application on your Flash Media Server server
                //values: 'rtmp://localhost/avconference12/_definst_', 'rtmp://myfmsserver.com/avconference12/room17/', etc...
                string connectionstring = repo.FindString("avc_connectionstring", "");

                //######################### OPTIONAL FIELDS (GENERAL) ###############
                ////fontsize:Number
                ////description: the size of the font used in teh text chat window
                ////values: 10,11,12,etc...
                int fontsize = repo.FindInt("avc_fontsize", 11);

                ////applyLangaugeFilter:String
                ////description:activates or deactivates bad language filter
                ////values: 0,1
                ////default: 1
                int applyLangaugeFilter = repo.FindInt("avc_applyLangaugeFilter", 1);

                ////autoStartMyCamera:Number
                ////description: if set to 1, when a user has a webcamera, immediatley after connecting his camera will start broadcasting, if set to 0, the user will have to press the "Start my camera" button to start it
                ////values 0,1 
                ////default :0
                int autoStartMyCamera = repo.FindInt("avc_autoStartMyCamera", 0);

                ////usnminchars
                ////description: the minimum numbers of characters a username must have, if you provide 0 or a negative value it will default to 1
                ////values: 3,4,5.. any number
                ////defalt: 3
                int usnminchars = repo.FindInt("avc_usnminchars", 3);

                ////username:String
                ////description: the default username to be used in the Login NICK log in box in the flash user interface
                ////values:only words with characters in this range: 0..9,a..z,A..Z,@,.
                ////default://
                string username = userName; //repo.FindString("avc_username", "");

                ////changeuser:Boolean
                ////description: allows or denies a user to edit his nickname in the NICK log in box in the flash user interface
                ////values: 0, 1
                ////default: 1
                int changeuser = repo.FindInt("avc_changeuser", 1);

                ////gender:String
                ////description: the default gender to be used in the gender radio buttons in the flash user interface
                ////values: 'male', 'female', 'couple'
                ////default selected: 'male'
                string gender = repo.FindString("avc_gender", "");

                ////changegender:Boolean
                ////description: allows or denies a user to modify his gender in the NICK log in box in the flash user interface
                ////values: 0,1
                ////default: 1
                int changegender = repo.FindInt("avc_changegender", 1);

                ////privatemessages:Number
                ////decription: enable/disable the PM function
                ////values: 0,1
                ////default: 1 (enabled)
                int privatemessages = repo.FindInt("avc_privatemessages", 1);

                ////jlmessages:Number
                ////description: set to 0 if you do not want all those 'joined', 'left' messages to show up in the text chat area
                ////values: 0,1
                ////default: 1
                int jlmessages = repo.FindInt("avc_jlmessages", 1);

                ////autoStartCameras
                ////description: if set to 1 as soon as a person starts his webcam, all the other persons logged in will start viewing it, also if set to 1 , when you log in all the avaialable cameras will start playing. Set to 1 for coneference scenarios
                ////values: 0,1 
                ////default:1 
                int autoStartCameras = repo.FindInt("avc_autoStartCameras", 1);

                ////bandwidthurl
                ////description: relative path to the .xml file containing video settings. Use it to give some users the ability to send higher quality video than others.
                ////default: "audio_video_quality_profiles/256k_high_motion_low_picture_quality.xml"
                string bandwidthurl = repo.FindString("avc_bandwidthurl", "audio_video_quality_profiles/256k_high_motion_low_picture_quality.xml");

                ////playSoundOnmessage
                ////description: set to 1 so that a sound will play every time a text message comes in
                ////values: 0,1
                ////default:1
                int playSoundOnmessage = repo.FindInt("avc_playSoundOnmessage", 1);

                ////showTimeStamps
                ////description: if set to 1 in the text chat window the time at which each text message camed in will be displayed
                ////values: 0,1
                ////default:1
                int showTimeStamps = repo.FindInt("avc_showTimeStamps", 1);

                ////maxUploadFileSize
                ////description: the maximum size (in bytes) of the files that can be uploaded to the Shared Folder
                ////values: numbers, bytes
                ////default:524288 (=0.5 MB)
                int maxUploadFileSize = repo.FindInt("avc_maxUploadFileSize", 524288);

                ////fileSharingEnabled
                ////description: if set to 0 the "Shared Files" window will not show up in the chat interface
                ////values: 0,1
                ////default:1
                int fileSharingEnabled = repo.FindInt("avc_fileSharingEnabled", 1);

                ////maxcams
                ////description: the max number of cams a person can watch
                ////values: 0,1,2,3,etc...
                ////default:4
                int maxcams = repo.FindInt("avc_maxcams", 4);

                ////allowSendAudioVideo
                ////description: set to 0 if the user should not eb able to send video and audio
                ////values: 0,1
                ////default:1
                int allowSendAudioVideo = repo.FindInt("avc_allowSendAudioVideo", 1);

                ////coupleGenderEnabled
                ////description: set to 0 if you want to hide the couple gender option
                ////values:1,0
                ////default:1
                int coupleGenderEnabled = repo.FindInt("avc_coupleGenderEnabled", 1);

                ////maxIncomingVideoSpeed:Number
                ////description: 
                ////values: -1
                ////default:-1
                int maxIncomingVideoSpeed = repo.FindInt("avc_maxIncomingVideoSpeed", -1);

                ////freeVideoTime:Number
                ////description: the ammount of time (in seconds) that a "free" user can view other videos in 1 day. Each "day" starts at midnight(00:00:00) and ends at 23:59:59 the same day, based on the clock of the users computer. Set to -1 to disable it.
                ////values:seconds
                ////default:6000
                int freeVideoTime = repo.FindInt("avc_freeVideoTime", 6000);

                ////secondsBetweenBuzzez:Number
                ////description: after using the [BUZZ] button , it il be disabled for a few seconds, use this variable to set the ammount of seconds
                ////values:0,1,10,etc...
                ////default:10
                int secondsBetweenBuzzez = repo.FindInt("avc_secondsBetweenBuzzez", 10);

                ////backgroundImageUrl
                ////description: the URl to a jpeg, gif, png image or a swf animation, the fiel needs to be on the same exact (sub)domain as the swf file
                ////values: image.jpg, animation.swf, transparency.png
                ////default: "pattern_061.gif"
                string backgroundImageUrl = repo.FindString("avc_backgroundImageUrl", "pattern_061.gif");

                ////backgroundImageAlpha
                ////backgroundImageAlpha: the opacity level of the background image, set to 100 for a non transparent background image, set to 0 for a fully transparent background image
                ////values:0 to 100
                ////default:100
                int backgroundImageAlpha = repo.FindInt("avc_backgroundImageAlpha", 100);

                ////backgroundImageScale
                ////description: shoudl the background image specified above scale to the width and height of the .swf file when the swf file is loaded in the browser. A good source for tile images is http://squidfingers.com/patterns
                ////values:none, tile,fit
                ////default:tile
                string backgroundImageScale = repo.FindString("avc_backgroundImageScale", "tile");

                ////highlightEmoteIcons:Number
                ////description: should emoteicons like text be bolded by default or left alone
                ////values:0,1
                ////default:1
                int highlightEmoteIcons = repo.FindInt("avc_highlightEmoteIcons", 1);

                ////tooltipsEnabled
                ////description: set to 0 if you want to disable the tooltips
                ////values:0,1
                ////default:1
                int tooltipsEnabled = repo.FindInt("avc_tooltipsEnabled", 1);

                ////adminIsVisible:Number
                ////description: when the admin will log in will his visible or invisible radio button be checked?
                ////values:1 so that the 'invisible' checkbox is selected,0 so that the 'visible' checkbox is selected
                ////default:0 //visible
                int adminIsVisible = repo.FindInt("avc_adminIsVisible", 0);

                ////toggleVisibilityEnabled:Number
                ////description: is the admin allowed to change its visibility settings set above
                ////values:0,1
                ////default:1
                int toggleVisibilityEnabled = repo.FindInt("avc_toggleVisibilityEnabled", 1);

                ////sortorder:String
                ////description: teh way the list of users is sorted, set to camsfirst so that users with cams/microphones activated are placed at the top of the list
                ////values:alphabetical,camsfirst
                ////default:alphabetical
                string sortorder = repo.FindString("avc_sortorder", "alphabetical"); 

                ////disableGenderSelection:Number
                ////description: if set to 1, gender options from the login screen are hidden and everyone is considered a male, has no effect on admins
                ////values:1,0
                ////default:0
                int disableGenderSelection = repo.FindInt("avc_disableGenderSelection", 0);

                ////adminCanBan
                ////description: can the admin ban people?! 
                ////values: 0 for no , 1 for yes
                ////default:1
                int adminCanBan = repo.FindInt("avc_adminCanBan", 1);

                ////adminCanKick
                ////description: can the admin kick people?! 
                ////values: 0 for no , 1 for yes
                ////default:1
                int adminCanKick = repo.FindInt("avc_adminCanKick", 1);

                ////adminCanViewPrivatePM
                ////description: can the admin view private PM's exchanged between people?! 
                ////values: 0 for no , 1 for yes
                ////default:1
                int adminCanViewPrivatePM = repo.FindInt("avc_adminCanViewPrivatePM", 1);

                ////buzzButtonEnabled:Number
                ////description : is the BUZZ button enabled in the flash interface?!
                ////values :0,1
                ////default: 1
                int buzzButtonEnabled = repo.FindInt("avc_buzzButtonEnabled", 1);

                ////allowLinksInTextChat
                ////description: shoul you turn this feature off, links will be censured from the text chat
                ////values: 0 for no , 1 for yes
                ////default:1
                int allowLinksInTextChat = repo.FindInt("avc_allowLinksInTextChat", 1);

                ////textChatCharLimit
                ////description: the number a characters the input text box for the text chat (im) and public supports, use for limiting spamming and repeated messages, the admin gets twice as much
                ////values: numbers
                ////default:200
                int textChatCharLimit = repo.FindInt("avc_textChatCharLimit", 200); 

                ////showLoginError
                ////description: if set to 1, the chat interface will display a "need to be logged in message" and will not let the user connect, good for when integrating
                ////value:0,1
                ////default:0
                int showLoginError = repo.FindInt("avc_showLoginError", 0); 

                ////showIps
                ////description: if set to 1, the chat interface will display the ips of the clients in the userslist
                ////value:0,1
                ////default:0
                int showIps = repo.FindInt("avc_showIps", 0); 

                ////floatingMode
                ////floatingMode: if set to 1 the avconference area will be split into 2 vertically, the text+userslist+filesharing panels will occupy the bottom 50%, the webcams will be centered vertically in the top 50%
                ////value:0,1
                ////default: 0
                int floatingMode = repo.FindInt("avc_floatingMode", 0); 

                ////videoChatOnly
                ////description: if set to 1 the text chat, users list and file sharing panel are hidden
                ////value:0,1
                ////default: 0
                int videoChatOnly = repo.FindInt("avc_videoChatOnly", 0); 

                //languageCode:String
                //description: used to specify the language to be used in AVConference. 
                //description: If you change it to "fr" for example you need to have a "fr" folder and 2 xml files in that folder: "fr/index_fr.xml" and "fr/admin_fr.xml", AVConference will load these xml files and use them.
                //default: "en"
                string languageCode = repo.FindString("avc_languageCode", "en"); 

                ////windowsColor:String
                ////description: the hex color of the windows inside AVConference
                ////default: "0x171717"
                string windowsColor = repo.FindString("avc_windowsColor", "0x171717");

                ////sendColor:String
                ////description: the hex color of the Send Text Chat Button inside AVConference
                ////default: "0xff0000"
                string sendColor = repo.FindString("avc_sendColor", "0xff0000"); 

                //userCanDragWindows
                //description: weather or not the windows inside AVConference are draggable
                //values: 1 for true, 0 for false
                //default: 1
                int userCanDragWindows = repo.FindInt("avc_userCanDragWindows", 1); 

                //autoCloseCams:Number
                //description: if set to 1, when a user I'm watching leaves the room, his webcam will be removed from the stage instead of saying "this user has left the room" or "this user has stopped broadcasting"
                //values 0,1 
                //default :0
                int autoCloseCams = repo.FindInt("avc_autoCloseCams", 0); 

                //disableCameraWindowResize:Number
                //description: if set to 1 the user will no longer be able to resize the camera window.
                //values 0,1 
                //default :0
                int disableCameraWindowResize = repo.FindInt("avc_disableCameraWindowResize", 0); 

                //windowsFontColor:String
                //description: the hex color of the text of the windows inside AVConference
                //default: "0xcccccc"
                string windowsFontColor = repo.FindString("avc_windowsFontColor", "0xcccccc");

                //hideUsersList:String
                //description: this setting controls whether or not the users list is hidden or not.
                //values 1 for hidden, 0 to show it.
                //default: 0
                int hideUsersList = repo.FindInt("avc_hideUsersList", 0); 

                //errorTextColor:String
                //description: the hex color of the errors that are shown by AVConference.
                //default: "0xffffff"
                string errorTextColor = repo.FindString("avc_errorTextColor", "0xffffff");

                //loginTextColor:String
                //description: the hex color of the texts shown at login;
                //default: "0xffffff"
                string loginTextColor = repo.FindString("avc_loginTextColor", "0xffffff");

                // integration.php code
                //backgroundImageUrl = "";

                if (randomVar == 1)
                {
                    connectionstring = repo.FindString("avc_connectionstring_" + randomVar.ToString(), "rtmp://184.82.130.66/avconference12/video");
                    bandwidthurl = repo.FindString("avc_bandwidthurl_" + randomVar.ToString(), "audio_video_quality_profiles/512k_high_motion_medium_picture_quality_high_sound_quality_640by480.xml");

                    allowSendAudioVideo = repo.FindInt("avc_allowSendAudioVideo_" + randomVar.ToString(), 1);
                    videoChatOnly = repo.FindInt("avc_videoChatOnly_" + randomVar.ToString(), 1);
                    freeVideoTime = repo.FindInt("avc_freeVideoTime_" + randomVar.ToString(), -1);
                    disableGenderSelection = repo.FindInt("avc_disableGenderSelection_" + randomVar.ToString(), 1); 
                    //username = repo.FindString("avc_username_" + randomVar.ToString(), "Broadcaster");
                    changeuser = repo.FindInt("avc_changeuser_" + randomVar.ToString(), 0); 
                }
                else if (randomVar == 2)
                {
                    connectionstring = repo.FindString("avc_connectionstring_" + randomVar.ToString(), "rtmp://184.82.130.66/avconference12/video");
                    bandwidthurl = repo.FindString("avc_bandwidthurl_" + randomVar.ToString(), "audio_video_quality_profiles/512k_high_motion_medium_picture_quality_high_sound_quality_640by480.xml");

                    allowSendAudioVideo = repo.FindInt("avc_allowSendAudioVideo_" + randomVar.ToString(), 0);
                    autoStartCameras = repo.FindInt("avc_autoStartCameras_" + randomVar.ToString(), 1);
                    videoChatOnly = repo.FindInt("avc_videoChatOnly_" + randomVar.ToString(), 1);
                    //these can be changed appropriately
                    maxcams = repo.FindInt("avc_maxcams_" + randomVar.ToString(), 4);
                    freeVideoTime = repo.FindInt("avc_freeVideoTime_" + randomVar.ToString(), -1);
                    disableGenderSelection = repo.FindInt("avc_disableGenderSelection_" + randomVar.ToString(), 1); 
                    //username = repo.FindString("avc_username_" + randomVar.ToString(), "Viewer");
                    changeuser = repo.FindInt("avc_changeuser_" + randomVar.ToString(), 0); 
                }
                else if (randomVar == 3)
                {
                    connectionstring = repo.FindString("avc_connectionstring_" + randomVar.ToString(), "rtmp://184.82.130.66/avconference12/conference");
                    bandwidthurl = repo.FindString("avc_bandwidthurl_" + randomVar.ToString(), "audio_video_quality_profiles/512k_high_motion_medium_picture_quality_high_sound_quality_320by240.xml");
                    //normal user
                    allowSendAudioVideo = repo.FindInt("avc_allowSendAudioVideo_" + randomVar.ToString(), 0);
                    autoStartCameras = repo.FindInt("avc_autoStartCameras_" + randomVar.ToString(), 1);
                    freeVideoTime = repo.FindInt("avc_freeVideoTime_" + randomVar.ToString(), -1);
                    disableGenderSelection = repo.FindInt("avc_disableGenderSelection_" + randomVar.ToString(), 1); 
                    //username = repo.FindString("avc_username_" + randomVar.ToString(), "User");
                    changeuser = repo.FindInt("avc_changeuser_" + randomVar.ToString(), 0);
                    fileSharingEnabled = repo.FindInt("avc_fileSharingEnabled_" + randomVar.ToString(), 0); 

                }
                else if (randomVar == 32)
                {

                    connectionstring = repo.FindString("avc_connectionstring_" + randomVar.ToString(), "rtmp://184.82.130.66/avconference12/conference");
                    bandwidthurl = repo.FindString("avc_bandwidthurl_" + randomVar.ToString(), "audio_video_quality_profiles/512k_high_motion_medium_picture_quality_high_sound_quality_320by240.xml");
                    //admin user
                    allowSendAudioVideo = repo.FindInt("avc_allowSendAudioVideo_" + randomVar.ToString(), 1);
                    autoStartMyCamera = repo.FindInt("avc_autoStartMyCamera_" + randomVar.ToString(), 1);
                    freeVideoTime = repo.FindInt("avc_freeVideoTime_" + randomVar.ToString(), -1);
                    //username = repo.FindString("avc_username_" + randomVar.ToString(), "Presenter");
                    changeuser = repo.FindInt("avc_changeuser_" + randomVar.ToString(), 0);
                    disableGenderSelection = repo.FindInt("avc_disableGenderSelection_" + randomVar.ToString(), 1);
                    fileSharingEnabled = repo.FindInt("avc_fileSharingEnabled_" + randomVar.ToString(), 0); 
                }



                //##################### DO NOT EDIT BELOW ############################
                page.Response.Clear();
                page.Response.Write("&connectionstring=");
                page.Response.Write(connectionstring);
                page.Response.Write("&fontsize=");
                page.Response.Write(fontsize);
                page.Response.Write("&applyLangaugeFilter=");
                page.Response.Write(applyLangaugeFilter);
                page.Response.Write("&autoStartMyCamera=");
                page.Response.Write(autoStartMyCamera);
                page.Response.Write("&usnminchars=");
                page.Response.Write(usnminchars);
                page.Response.Write("&username=");
                page.Response.Write(username);
                page.Response.Write("&changeuser=");
                page.Response.Write(changeuser);
                page.Response.Write("&gender=");
                page.Response.Write(gender);
                page.Response.Write("&changegender=");
                page.Response.Write(changegender);
                page.Response.Write("&privatemessages=");
                page.Response.Write(privatemessages);
                page.Response.Write("&jlmessages=");
                page.Response.Write(jlmessages);
                page.Response.Write("&autoStartCameras=");
                page.Response.Write(autoStartCameras);
                page.Response.Write("&bandwidthurl=");
                page.Response.Write(bandwidthurl);
                page.Response.Write("&playSoundOnmessage=");
                page.Response.Write(playSoundOnmessage);
                page.Response.Write("&showTimeStamps=");
                page.Response.Write(showTimeStamps);
                page.Response.Write("&maxUploadFileSize=");
                page.Response.Write(maxUploadFileSize);
                page.Response.Write("&fileSharingEnabled=");
                page.Response.Write(fileSharingEnabled);
                page.Response.Write("&maxcams=");
                page.Response.Write(maxcams);
                page.Response.Write("&allowSendAudioVideo=");
                page.Response.Write(allowSendAudioVideo);
                page.Response.Write("&coupleGenderEnabled=");
                page.Response.Write(coupleGenderEnabled);
                page.Response.Write("&maxIncomingVideoSpeed=");
                page.Response.Write(maxIncomingVideoSpeed);
                page.Response.Write("&freeVideoTime=");
                page.Response.Write(freeVideoTime);
                page.Response.Write("&secondsBetweenBuzzez=");
                page.Response.Write(secondsBetweenBuzzez);
                page.Response.Write("&backgroundImageUrl=");
                page.Response.Write(backgroundImageUrl);
                page.Response.Write("&backgroundImageAlpha=");
                page.Response.Write(backgroundImageAlpha);
                page.Response.Write("&backgroundImageScale=");
                page.Response.Write(backgroundImageScale);
                page.Response.Write("&highlightEmoteIcons=");
                page.Response.Write(highlightEmoteIcons);
                page.Response.Write("&tooltipsEnabled=");
                page.Response.Write(tooltipsEnabled);
                page.Response.Write("&adminIsVisible=");
                page.Response.Write(adminIsVisible);
                page.Response.Write("&toggleVisibilityEnabled=");
                page.Response.Write(toggleVisibilityEnabled);
                page.Response.Write("&disableGenderSelection=");
                page.Response.Write(disableGenderSelection);
                page.Response.Write("&sortorder=");
                page.Response.Write(sortorder);
                page.Response.Write("&adminCanBan=");
                page.Response.Write(adminCanBan);
                page.Response.Write("&adminCanKick=");
                page.Response.Write(adminCanKick);
                page.Response.Write("&adminCanViewPrivatePM=");
                page.Response.Write(adminCanViewPrivatePM);
                page.Response.Write("&buzzButtonEnabled=");
                page.Response.Write(buzzButtonEnabled);
                page.Response.Write("&allowLinksInTextChat=");
                page.Response.Write(allowLinksInTextChat);
                page.Response.Write("&textChatCharLimit=");
                page.Response.Write(textChatCharLimit);
                page.Response.Write("&showLoginError=");
                page.Response.Write(showLoginError);
                page.Response.Write("&showIps=");
                page.Response.Write(showIps);
                page.Response.Write("&floatingMode=");
                page.Response.Write(floatingMode);
                page.Response.Write("&videoChatOnly=");
                page.Response.Write(videoChatOnly);
                page.Response.Write("&languageCode=");
                page.Response.Write(languageCode);
                page.Response.Write("&windowsColor=");
                page.Response.Write(windowsColor);
                page.Response.Write("&sendColor=");
                page.Response.Write(sendColor);
                page.Response.Write("&userCanDragWindows=");
                page.Response.Write(userCanDragWindows);
                page.Response.Write("&autoCloseCams=");
                page.Response.Write(autoCloseCams);
                page.Response.Write("&disableCameraWindowResize=");
                page.Response.Write(disableCameraWindowResize);
                page.Response.Write("&windowsFontColor=");
                page.Response.Write(windowsFontColor);
                page.Response.Write("&hideUsersList=");
                page.Response.Write(hideUsersList);
                page.Response.Write("&errorTextColor=");
                page.Response.Write(errorTextColor);
                page.Response.Write("&loginTextColor=");
                page.Response.Write(loginTextColor);
                page.Response.End();
            }
        }

        public static void FileList(string folder, Page page)
        {
            string instanceName = "";
            if (!string.IsNullOrEmpty(page.Request.QueryString["instanceName"]))
                instanceName = page.Request.QueryString["instanceName"];
            folder = Path.Combine(folder, instanceName);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<xml version=\"1.0\" encoding=\"UTF-8\">");
            DirectoryInfo di = new DirectoryInfo(folder);
            foreach (FileInfo fi in di.GetFiles("*.*", SearchOption.AllDirectories))
            {
                if ((fi.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                    continue;

                sb.AppendLine(string.Format("<file name=\"{0}\" size=\"{1}\" path=\"{2}\"></file>", fi.Name, fi.Length.ToString(), "uploadedFiles/" + (string.IsNullOrEmpty(instanceName)?"":instanceName + "/") + fi.Name));
            }
            sb.AppendLine("</xml>");

            page.Response.Clear();
            page.Response.Write(sb.ToString());
            page.Response.End();

        }

        public static void Upload(string folder, Page page)
        {
            string instanceName = "";
            if (!string.IsNullOrEmpty(page.Request.QueryString["instanceName"]))
                instanceName = page.Request.QueryString["instanceName"];
            folder = Path.Combine(folder, instanceName);

            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            
            foreach (HttpPostedFile file in page.Request.Files) {
                string filePath = Path.Combine(folder, file.FileName);
                file.SaveAs(filePath);
            }
        }

        public static void WhiteUserList(string folder, Page page)
        {
            NameValueCollection postData = page.Request.Form;
            
            string instanceName = "";
            if (!string.IsNullOrEmpty(postData["instance"]))
                instanceName = postData["instance"];

            string fileName = Path.Combine(folder, "users_" + instanceName + ".txt");
            if (!string.IsNullOrEmpty(postData["nr_of_users"]))
            {
                try
                {
                    using (StreamWriter sw = File.CreateText(fileName))
                    {
                        sw.Write(postData["nr_of_users"]);
                    }	

                    page.Response.Write("Message from the writeuserslist script: Success, wrote users to file .txt");
                }
                catch
                {
                    page.Response.Write("Message from the writeuserslist script: Cannot write to file .txt");
                }
            }

            fileName = Path.Combine(folder, "users_" + instanceName + ".xml");
            if (!string.IsNullOrEmpty(postData["xml"]))
            {
                try
                {
                    using (StreamWriter sw = File.CreateText(fileName))
                    {
                        sw.Write(HttpUtility.UrlDecode(postData["xml"]));
                    }

                    page.Response.Write("Message from the writeuserslist script: Success, wrote users to file .xml");
                }
                catch
                {
                    page.Response.Write("Message from the writeuserslist script: Cannot write to file .xml");
                }
            }
        }
    }
}